var ModalHelper = (function(bodyCls) {
    var scrollTop;
    return {
        afterOpen: function() {
            scrollTop = document.scrollingElement.scrollTop;
            document.body.classList.add(bodyCls);
            document.body.style.top = -scrollTop + 'px';
        },
        beforeClose: function() {
            document.body.classList.remove(bodyCls);
            document.scrollingElement.scrollTop = scrollTop;
        }
    };
})('gfixed');
//样式选择
    $('.styleText ').click(function(){
      $(this).addClass('styleColor').siblings().removeClass('styleColor');
    })
    //选择模板
    $('.footer_moban').click(function(){
      $(this).addClass('footer_Color').siblings().removeClass('footer_Color');
      $('.moban').removeClass('none').siblings().addClass('none');
       $('.tiaozheng').hide();
       $(".textarea").attr("readonly","readonly");

    })
    //选择封面
    $('.footer_cover').click(function(){
      $(this).addClass('footer_Color').siblings().removeClass('footer_Color');
      $('.main_cover').removeClass('none').siblings().addClass('none');
      $('.tiaozheng').hide();
      $(".textarea").attr("readonly","readonly");

    })
    //编辑内容
    $('.footer_edit').click(function(){
      $('.main_bot').removeClass('none').siblings().addClass('none');
      $(this).addClass('footer_Color').siblings().removeClass('footer_Color');
      $('.tiaozheng').show();
      $(".textarea").attr("readonly",false);

    })

    //封面的切换
    $('.cover_style_img').click(function(){
      $('.cover_style_top').removeClass('cover_cur');
      $(this).siblings().addClass('cover_cur');

    })




var zong1 = $('.top_container').length
        $('.zongshu').text(zong1);
        $('#gundong').attr('max',zong1);


//选择模板
$('.footer_moban').click(function(){
	$('.tiaozheng').hide();
})
//编辑封面
$('.footer_cover').click(function(){
	$('.tiaozheng').hide();
})
//编辑内容
$('.footer_edit').click(function(){
	$('.tiaozheng').show();

})

//点击确定
function sure(obj,id){
    var param={}
    param.photoId=id;
    param.zuo=$(obj).parent().parent().siblings('img').css('marginLeft');
    param.top=$(obj).parent().parent().siblings('img').css('marginTop');
//    alert(JSON.stringify(param))
    $.post(Think.U('WebApp/PhotoBooks/saveLittleTrim'),param, function(data){
        if(data.status==1){
            $('.weitiao div').removeClass('Zindex');
             $('.fee100').removeClass('bag');
            $('.wt').hide();
            $('.feel_zhao').hide();
            $('.tiaozheng').show();
        }
    })

}

function weitiao(obj,number){
    $(obj).parent().prev().children('.wt').show();
    $(obj).parent().prev().show();
    $(obj).parent().hide();
    var kai =$(obj).parent().parent().parent().siblings().children('.fee100');
    kai.children('.tiaozheng').hide();
    kai.children('.feel_zhao').show();
    //kai.children('.feel_zhao').children('.weitiao').children('.feel_huan').addClass('Zindex');
    //$(obj).parent().prev().children('.weitiao ').children('.wei_img ').addClass('Zindex');
    //$(obj).parent().parent().addClass('bag');
    $(obj).parent().parent().attr('data-number',number);
    //var bbleft =$(obj).parent().siblings('img').css('marginLeft');
    ///var bbtop=$(obj).parent().siblings('img').css('marginTop');
    console.log(bbleft+'bbleft');
    console.log(bbtop+'bbtop');
}

function huanwei(obj,number){
    var param={}
    var page=$('.yeshu').text();
    param.newNumber=number;
    param.oldNumber=$('.bag').attr('data-number');
    param.photoBookId=$('#pbId').val();
    param.inPage=page;
//    alert(JSON.stringify(obj))
//    return;
    $.post(Think.U('WebApp/PhotoBooks/togglePhoto'),param, function(json){
//        alert(JSON.stringify(json))
        if(json.status==1){
            localStorage.setItem('curPage',page);
            var src = $(obj).parent().parent().parent().children('img');
            var sibSrc = $(obj).parent().parent().parent().parent().siblings().children('.bag').children('img');
            var url =sibSrc.attr('src');
            var Surl =src.attr('src')

            src.attr('src',url);
            sibSrc.attr('src',Surl);
            $(obj).parent().parent().parent().parent().siblings().children('.fee100').removeClass('bag');
            $('.weitiao div').removeClass('Zindex');
            $('.wt').hide();
            $('.feel_zhao').hide();
            $('.tiaozheng').show();
        }
    })

}
////点击删除
function deleteImg(param,id){
    var page=$('.yeshu').text();//页id
    var pbId=$('#pbId').val();//书id
    var zong = $(".zongshu").text();
    $.post(Think.U('WebApp/PhotoBooks/delPhoto'),{photoId:id,page:page,pbId:pbId}, function(data){
        console.log(data);
        var str='';
        if(data.status==1){
            //把删除的图放到预留位置
            str+='<div class="fang left">';
            str+='<div class="new_book select_'+data.nulPhoto+'">';
            str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/select.png" class="new_img">';
            str+='<div class="new_text new_left" onclick="inNewPage('+id+',this)">新起页</div>';
            str+='<div class="new_text new_right" onclick="sureDelPhoto('+id+',this)">删除</div>';
            str+='</div>';
            str+='<div class="new_center center_'+data.nulPhoto+'" onclick="inBook('+id+',this)">当前页</div>';
            str+='<img src="'+WST.ROOT+'/'+data.url+'" class="img" onclick="showSelect('+data.nulPhoto+')">';
            str+='</div>';
            $('#reserved').append(str)
            $(param).parent().parent().parent().remove();
            //再更新当前页的样式
            var status=$('.status'+page).val();
            var curPage=['One','Two','Three','Four','Five']
            var zimu=['a','b','c','d','e'];
            var len=Number($('.numBer'+page).length-1)
            var old=len+1
            for(var i=0;i<old;i++){
                $('.numBer'+page).eq(i).removeClass(zimu[old]+'_moveOne_'+status);
                $('.numBer'+page).eq(i).removeClass(zimu[old]+'_moveTwo_'+status);
                $('.numBer'+page).eq(i).removeClass(zimu[old]+'_moveThree_'+status);
                $('.numBer'+page).eq(i).removeClass(zimu[old]+'_moveFour_'+status);
                $('.numBer'+page).eq(i).removeClass(zimu[old]+'_moveFive_'+status);
                $('.numBer'+page).eq(i).addClass(zimu[len]+'_move'+curPage[i]+'_'+status);

                $('.text'+page+'_top').eq(i).removeClass(zimu[old]+'_text_'+status+'_top');
                $('.text'+page+'_bottom').eq(i).removeClass(zimu[old]+'_text_'+status+'_bottom');
                $('.text'+page+'_top').eq(i).addClass(zimu[len]+'_text_'+status+'_top');
                $('.text'+page+'_bottom').eq(i).addClass(zimu[len]+'_text_'+status+'_bottom');
            }
            window.location.reload();
        }else if(data.status==2){   //清空这一页内容
            if(zong == 1){
                localStorage.setItem('uploadImgPage'+pbId,zong);
                localStorage.setItem('curPage'+pbId,zong);
            }else{
                localStorage.setItem('uploadImgPage'+pbId,Number(zong)-1);
                var curPage = localStorage.getItem('curPage'+pbId);
                if(Number(curPage)>Number(zong)-1){
                    localStorage.setItem('curPage'+pbId,Number(zong)-1);
                }
            }
            window.location.reload()
        }else if(data.status==3 || data.master===2){
            wstMsg("您无法删除其他人上传的图片!");
        }
    })

}

//点击切换排版   适用于两种版式互相切换
$('.qh').click(function(){
    var pbId = $("#pbId").val();
    var page=$('.yeshu').text();
    var obj={}
    var status=$('.status'+page).val();
    var newStatus=['2','1'];
	var curPage=['One','Two','Three','Four','Five']
	var zimu=['a','b','c','d','e'];
	var len=Number($('.numBer'+page).length-1)
    obj.status=newStatus[status-1];
    obj.photoBookId=$('#pbId').val();
    obj.inPage=page;
// alert(JSON.stringify(obj))
    $.post(Think.U('WebApp/PhotoBooks/movePhoto'),obj, function(json){
//alert(JSON.stringify(json))
        if(json.status==1){
            //localStorage.setItem('curPage'+pbId,page);
            if(status==1){
                for(var i=0;i<$('.numBer'+page).length;i++){
                    $('.numBer'+page).eq(i).removeClass(zimu[len]+'_move'+curPage[i]+'_1');
                    $('.numBer'+page).eq(i).addClass(zimu[len]+'_move'+curPage[i]+'_2');
                }
                $('.status'+page).val(2);
                $('.text'+page+'_top').removeClass(zimu[len]+'_text_1_top');
                $('.text'+page+'_bottom').removeClass(zimu[len]+'_text_1_bottom');
                $('.text'+page+'_top').addClass(zimu[len]+'_text_2_top');
                $('.text'+page+'_bottom').addClass(zimu[len]+'_text_2_bottom');
                if(zimu[len]=='a'){
                    $('.a_text_2_top .text'+page).attr("max-length","150");
                    $('.a_text_2_bottom .text'+page).attr("max-length","150");
                }else if(zimu[len]=='b'){
                    $('.b_text_2_top .text'+page).attr("max-length","50");
                    $('.b_text_2_bottom .text'+page).attr("max-length","50");
                }else if(zimu[len]=='c'){
                    $('.c_text_2_top .text'+page).attr("max-length","50");
                    $('.c_text_2_bottom .text'+page).attr("max-length","50");
                }else if(zimu[len]=='d'){
                    $('.d_text_2_top .text'+page).attr("max-length","50");
                    $('.d_text_2_bottom .text'+page).attr("max-length","50");
                }else if(zimu[len]=='e'){
                    $('.e_text_2_top .text'+page).attr("max-length","50");
                    $('.e_text_2_bottom .text'+page).attr("max-length","50");
                }
            }else{
                for(var i=0;i<$('.numBer'+page).length;i++){
                    $('.numBer'+page).eq(i).removeClass(zimu[len]+'_move'+curPage[i]+'_2');
                    $('.numBer'+page).eq(i).addClass(zimu[len]+'_move'+curPage[i]+'_1');
                }
                $('.status'+page).val(1);
                $('.text'+page+'_top').removeClass(zimu[len]+'_text_2_top');
                $('.text'+page+'_bottom').removeClass(zimu[len]+'_text_2_bottom');
                $('.text'+page+'_top').addClass(zimu[len]+'_text_1_top');
                $('.text'+page+'_bottom').addClass(zimu[len]+'_text_1_bottom');
                if(zimu[len]=='a'){
                    $('.a_text_1_top .text'+page).attr("max-length","150");
                    $('.a_text_1_bottom .text'+page).attr("max-length","150");
                }else if(zimu[len]=='b'){
                    $('.b_text_1_top .text'+page).attr("max-length","50");
                    $('.b_text_1_bottom .text'+page).attr("max-length","50");
                }else if(zimu[len]=='c'){
                    $('.c_text_1_top .text'+page).attr("max-length","50");
                    $('.c_text_1_bottom .text'+page).attr("max-length","50");
                }else if(zimu[len]=='d'){
                    $('.d_text_1_top .text'+page).attr("max-length","50");
                    $('.d_text_1_bottom .text'+page).attr("max-length","50");
                }else if(zimu[len]=='e'){
                    $('.e_text_1_top .text'+page).attr("max-length","50");
                    $('.e_text_1_bottom .text'+page).attr("max-length","50");
                }
            }
        }
    })
})


//显示图片加入书本选择
function showSelect(k){

  $('.new_book').hide();
    $('.new_center').hide();
    $('.select_'+k).show();
    $('.center_'+k).show();
}

//图放到新页面上
function inNewPage(id,param){
    var pbId=$('#pbId').val();
    $.post(Think.U('WebApp/PhotoBooks/addPageToBook'),{photoBookId:pbId,photoId:id}, function(json){
//        alert(JSON.stringify(json))
        if(json.status==1){
            localStorage.setItem('curPage'+pbId,json.newPage);
            $(param).parent().parent().remove();
            location.reload()
        }
    })
}
var first="";
//增加图片进页面
function inBook(id,param,direction){
    var page=$('.yeshu').text();
    //当前页的移动状态
    var curStatus=$('.status'+page).val();
    //先去后台判断当前页是否满5张照片
    var pbId=$('#pbId').val();
    $.post(Think.U('WebApp/PhotoBooks/checkPageIsFull'),{page:page,pbId:pbId,photoId:id}, function(json){
		//alert(json);

        var str='';
        //第一张照片有空位
        if(json.status==1){
            //移除预留的照片
            $(param).parent().remove();
            //数据库保存的照片并在书页上显示
            var obj={};
            obj.page=page;
            obj.photoId=id;
            obj.first=first;
            obj.type=1;
            if(direction){
                obj.direction = direction;
            }else{
                obj.direction=1;
            }
            $.post(Think.U('WebApp/PhotoBooks/photoInsertPage'),obj, function(data){
                //alert(JSON.stringify(data))
                first=data.ps;
                if(page==1){
                  location.reload();
                }else{
                    if(data.status==1){
                        str+='<div class="pirc numBer'+page+' a_moveOne_'+curStatus+'">';
                        str+='<div class="fee100">';
                        str+='<img src="'+WST.ROOT+'/'+json.img+'" class="pirc_img" data-shape="'+data.shape+'">';
                        str+='<div class="feel_zhao">';
                        str+='<div class="weitiao feel_absolute ">';
                        str+='<div class="wei_img feel_absolute ">';
                        /*str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_adjust_position_icon.png" class="weitiao_img">';
                        str+='<div class="wei_text">拖动微调</div>';*/
                        str+='</div>';
                        str+='<div class="feel_sure feel_absolute" onclick="sure(this,'+id+')">';
                        str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_carry_out_adjust_position_icon.png" class="weitiao_i">';
                        str+='<div class="wei_text">点击确认</div>';
                        str+='</div>';
                        str+='<div class="feel_huan feel_absolute" onclick="huanwei(this,\'One\')">';
                        str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_transposition_icon.png" class="weitiao_img">';
                        str+='<div class="wei_text">点击换位</div>';
                        str+='</div>';
                        str+='</div>';
                        str+='<div class="wt"></div>';
                        str+='</div>';
                        str+='<div class="tiaozheng">';
                        str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_delete_btn_nor.png" class="kai_delete" onclick="deleteImg(this,'+id+')">';
                        str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_adjust_position_btn_nor.png" class="kai_img"  onclick="weitiao(this,\'One\')">';
                        str+='</div>';
                        str+='</div></div>';
                    }
                    $('.imgDiv').eq(page-1).append(str);
                }

                //图片的移动
                var _x_start,_y_start,_x_move,_y_move,_x_end,_y_end,left_start,top_start, marginLeft, marginTop,width,height;

                //监听多个，相同的类
                var yiDong=[];
                var yidong = $('.wt');
                for(var k=0;k<yidong.length;k++){
                    yiDong.push(yidong[k]);
                    yiDong[k].addEventListener("touchstart",start);
                    yiDong[k].addEventListener("touchmove",move);
                    yiDong[k].addEventListener("touchend",end);
                    document.getElementsByClassName('wt')[k].addEventListener('touchend', function(e){
                        e.preventDefault();
                    });
                }
                //手指开始触摸
                function start(e){
                    _x_start=e.touches[0].pageX;
                    _y_start=e.touches[0].pageY;

                    left_start=$(this).parent().prev().css('marginLeft')
                    top_start=$(this).parent().prev().css('marginTop');

                    //console.log(top_start);
                }
                //手指移动
                function move(e){
                    _x_move=e.touches[0].pageX;
                    _y_move=e.touches[0].pageY;
                    marginLeft=parseInt(_x_move)-parseInt(_x_start)+parseInt(left_start);
                    marginTop=parseInt(_y_move)-parseInt(_y_start)+parseInt(top_start);
                    height=parseInt($(this).height()) * 0.05;
                    width= parseInt($(this).width()) * 0.1;
                    if(marginTop>20){
                        marginTop=parseInt($(this).height())*0.02;
                    }else if(marginTop<-20){
                        marginTop='-'+parseInt($(this).height())*0.02;
                    }
                    $(this).parent().prev().css("marginLeft",marginLeft+"px");
                    $(this).parent().prev().css("marginTop",marginTop+"px");
                }
                //手指离开
                function end(e){
                    var _x_end=e.changedTouches[0].pageX;
                    var _y_end=e.changedTouches[0].pageY;


                    $('body').removeClass('fixed');
                    $('.wt').hide();
                    $(this).prev().children('.wei_img').removeClass('Zindex');
                    $(this).prev().children('.feel_sure').addClass('Zindex');
                }
            });
            //window.location.reload();
        }else if(json.status==2){
            //移除预留的照片
            $(param).parent().remove();
            //数据库保存的照片并在书页上显示
            var obj={};
            obj.page=page;
            obj.photoId=id;
            if(direction){
                obj.direction = direction;
            }else{
                obj.direction=2;
            }
            $.post(Think.U('WebApp/PhotoBooks/photoInsertPage'),obj, function(data){
                location.reload();
                if(data.status==1){
                    $('.numBer'+page).eq(0).removeClass('a_moveOne_'+curStatus);
                    $('.numBer'+page).eq(0).addClass('b_moveOne_'+curStatus);
                    str+='<div class="pirc numBer'+page+' b_moveTwo_'+curStatus+'">';
                    str+='<div class="fee100">';
                    str+='<img src="'+WST.ROOT+'/'+json.img+'" class="pirc_img" data-shape="'+data.shape+'">';
                    str+='<div class="feel_zhao">';
                    str+='<div class="weitiao feel_absolute ">';
                    str+='<div class="wei_img feel_absolute ">';
                    /*str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_adjust_position_icon.png" class="weitiao_img">';
                    str+='<div class="wei_text">拖动微调</div>';*/
                    str+='</div>';
                    str+='<div class="feel_sure feel_absolute" onclick="sure(this,'+id+')">';
                    str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_carry_out_adjust_position_icon.png" class="weitiao_img">';
                    str+='<div class="wei_text">点击确认</div>';
                    str+='</div>';
                    str+='<div class="feel_huan feel_absolute" onclick="huanwei(this,\'Two\')">';
                    str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_transposition_icon.png" class="weitiao_img">';
                    str+='<div class="wei_text">点击换位</div>';
                    str+='</div>';
                    str+='</div>';
                    str+='<div class="wt"></div>';
                    str+='</div>';
                    str+='<div class="tiaozheng">';
                    str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_delete_btn_nor.png" class="kai_delete" onclick="deleteImg(this,'+id+')">';
                    str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_adjust_position_btn_nor.png" class="kai_img" onclick="weitiao(this,\'Two\')">';
                    str+='</div>';
                    str+='</div></div>';
                }
    
                $('.imgDiv').eq(page-1).append(str);
                //图片的移动

                var _x_start,_y_start,_x_move,_y_move,_x_end,_y_end,left_start,top_start, marginLeft, marginTop,width,height;

                //监听多个，相同的类
                var yiDong=[];
                var yidong = $('.wt');
                for(var k=0;k<yidong.length;k++){
                    yiDong.push(yidong[k]);
                    yiDong[k].addEventListener("touchstart",start);
                    yiDong[k].addEventListener("touchmove",move);
                    yiDong[k].addEventListener("touchend",end);
                    document.getElementsByClassName('wt')[k].addEventListener('touchend', function(e){
                        e.preventDefault();
                    });

                }
                //手指开始触摸
                function start(e){
                    _x_start=e.touches[0].pageX;
                    _y_start=e.touches[0].pageY;

                    left_start=$(this).parent().prev().css('marginLeft')
                    top_start=$(this).parent().prev().css('marginTop');

                    console.log(top_start);

                }

                //手指移动
                function move(e){
                    _x_move=e.touches[0].pageX;
                    _y_move=e.touches[0].pageY;
                    marginLeft=parseInt(_x_move)-parseInt(_x_start)+parseInt(left_start);
                    marginTop=parseInt(_y_move)-parseInt(_y_start)+parseInt(top_start);
                    height=parseInt($(this).height()) * 0.05;
                    width= parseInt($(this).width()) * 0.1;
                    if(marginTop>20){
                        marginTop=parseInt($(this).height())*0.02;
                    }else if(marginTop<-20){
                        marginTop='-'+parseInt($(this).height())*0.02;
                    }
                    $(this).parent().prev().css("marginLeft",marginLeft+"px");
                    $(this).parent().prev().css("marginTop",marginTop+"px");
                    //console.log(parseInt(_y_move)-parseInt(_y_start)+parseInt(top_start));
                }
                //手指离开
                function end(e){
                    var _x_end=e.changedTouches[0].pageX;
                    var _y_end=e.changedTouches[0].pageY;


                    $('body').removeClass('fixed');
                    $('.wt').hide();
                    $(this).prev().children('.wei_img').removeClass('Zindex');
                    $(this).prev().children('.feel_sure').addClass('Zindex');

                }
            });
                //location.reload();
        }else if(json.status==3){
            //移除预留的照片
            $(param).parent().remove();
            //数据库保存的照片并在书页上显示
            var obj={};
            obj.page=page;
            obj.photoId=id;
            if(direction){
                obj.direction = direction;
            }else{
                obj.direction=3;
            }
            $.post(Think.U('WebApp/PhotoBooks/photoInsertPage'),obj, function(data){
                //alert(JSON.stringify(data))
                location.reload();
                if(data.status==1){
                    $('.numBer'+page).eq(0).removeClass('b_moveOne_'+curStatus);
                    $('.numBer'+page).eq(0).addClass('c_moveOne_'+curStatus);
                    $('.numBer'+page).eq(1).removeClass('b_moveTwo_'+curStatus);
                    $('.numBer'+page).eq(1).addClass('c_moveTwo_'+curStatus);
                    str+='<div class="pirc numBer'+page+' c_moveThree_'+curStatus+'">';
                    str+='<div class="fee100">';
                    str+='<img src="'+WST.ROOT+'/'+json.img+'" class="pirc_img" data-shape="'+data.shape+'">';
                    str+='<div class="feel_zhao">';
                    str+='<div class="weitiao feel_absolute ">';
                    str+='<div class="wei_img feel_absolute ">';
                   /* str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_adjust_position_icon.png" class="weitiao_img">';
                    str+='<div class="wei_text">拖动微调</div>';*/
                    str+='</div>';
                    str+='<div class="feel_sure feel_absolute" onclick="sure(this,'+id+')">';
                    str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_carry_out_adjust_position_icon.png" class="weitiao_img">';
                    str+='<div class="wei_text">点击确认</div>';
                    str+='</div>';
                    str+='<div class="feel_huan feel_absolute" onclick="huanwei(this,\'Three\')">';
                    str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_transposition_icon.png" class="weitiao_img">';
                    str+='<div class="wei_text">点击换位</div>';
                    str+='</div>';
                    str+='</div>';
                    str+='<div class="wt"></div>';
                    str+='</div>';
                    str+='<div class="tiaozheng">';
                    str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_delete_btn_nor.png" class="kai_delete" onclick="deleteImg(this,'+id+')">';
                    str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_adjust_position_btn_nor.png" class="kai_img"  onclick="weitiao(this,\'Three\')">';
                    str+='</div>';
                    str+='</div></div>';
                }
                $('.imgDiv').eq(page-1).append(str);           
            });

        }else if(json.status==4){
            //移除预留的照片
            $(param).parent().remove();
            //数据库保存的照片并在书页上显示
            var obj={};
            obj.page=page;
            obj.photoId=id;
            if(direction){
                obj.direction = direction;
            }else{
                obj.direction=4;
            }
            $.post(Think.U('WebApp/PhotoBooks/photoInsertPage'),obj, function(data){
                location.reload();
                if(data.status==1){
                    $('.numBer'+page).eq(0).removeClass('c_moveOne_'+curStatus);
                    $('.numBer'+page).eq(0).addClass('d_moveOne_'+curStatus);
                    $('.numBer'+page).eq(1).removeClass('c_moveTwo_'+curStatus);
                    $('.numBer'+page).eq(1).addClass('d_moveTwo_'+curStatus);
                    $('.numBer'+page).eq(2).removeClass('c_moveThree_'+curStatus);
                    $('.numBer'+page).eq(2).addClass('d_moveThree_'+curStatus);
                    str+='<div class="pirc numBer'+page+' d_moveFour_'+curStatus+'">';
                    str+='<div class="fee100">';
                    str+='<img src="'+WST.ROOT+'/'+json.img+'" class="pirc_img" data-shape="'+data.shape+'">';
                    str+='<div class="feel_zhao">';
                    str+='<div class="weitiao feel_absolute ">';
                    str+='<div class="wei_img feel_absolute ">';
                   /* str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_adjust_position_icon.png" class="weitiao_img">';
                    str+='<div class="wei_text">拖动微调</div>';*/
                    str+='</div>';
                    str+='<div class="feel_sure feel_absolute" onclick="sure(this,'+id+')">';
                    str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_carry_out_adjust_position_icon.png" class="weitiao_img">';
                    str+='<div class="wei_text">点击确认</div>';
                    str+='</div>';
                    str+='<div class="feel_huan feel_absolute" onclick="huanwei(this,\'Four\')">';
                    str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_transposition_icon.png" class="weitiao_img">';
                    str+='<div class="wei_text">点击换位</div>';
                    str+='</div>';
                    str+='</div>';
                    str+='<div class="wt"></div>';
                    str+='</div>';
                    str+='<div class="tiaozheng">';
                    str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_delete_btn_nor.png" class="kai_delete" onclick="deleteImg(this,'+id+')">';
                    str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_adjust_position_btn_nor.png" class="kai_img"  onclick="weitiao(this,\'Four\')">';
                    str+='</div>';
                    str+='</div></div>';
                }
                $('.imgDiv').eq(page-1).append(str);
            });
                //location.reload();
        }else if(json.status==5){
            //移除预留的照片
            $(param).parent().remove();
            //数据库保存的照片并在书页上显示
            var obj={};
            obj.page=page;
            obj.photoId=id;
            if(direction){
                obj.direction = direction;
            }else{
                obj.direction=5;
            }
            //alert(obj.direction);
            $.post(Think.U('WebApp/PhotoBooks/photoInsertPage'),obj, function(data){
                //alert(JSON.stringify(data))
                location.reload();
                if(data.status==1){
                    $('.numBer'+page).eq(0).removeClass('d_moveOne_'+curStatus);
                    $('.numBer'+page).eq(0).addClass('e_moveOne_'+curStatus);
                    $('.numBer'+page).eq(1).removeClass('d_moveTwo_'+curStatus);
                    $('.numBer'+page).eq(1).addClass('e_moveTwo_'+curStatus);
                    $('.numBer'+page).eq(2).removeClass('d_moveThree_'+curStatus);
                    $('.numBer'+page).eq(2).addClass('e_moveThree_'+curStatus);
                    $('.numBer'+page).eq(3).removeClass('d_moveFour_'+curStatus);
                    $('.numBer'+page).eq(3).addClass('e_moveFour_'+curStatus);
                    str+='<div class="pirc numBer'+page+' e_moveFive_'+curStatus+'">';
                    str+='<div class="fee100">';
                    str+='<img src="'+WST.ROOT+'/'+json.img+'" class="pirc_img" data-shape="'+data.shape+'">';
                    str+='<div class="feel_zhao">';
                    str+='<div class="weitiao feel_absolute ">';
                    str+='<div class="wei_img feel_absolute ">';
                   /* str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_adjust_position_icon.png" class="weitiao_img">';
                    str+='<div class="wei_text">拖动微调</div>';*/
                    str+='</div>';
                    str+='<div class="feel_sure feel_absolute" onclick="sure(this,'+id+')">';
                    str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_carry_out_adjust_position_icon.png" class="weitiao_img">';
                    str+='<div class="wei_text">点击确认</div>';
                    str+='</div>';
                    str+='<div class="feel_huan feel_absolute" onclick="huanwei(this,\'Five\')">';
                    str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_transposition_icon.png" class="weitiao_img">';
                    str+='<div class="wei_text">点击换位</div>';
                    str+='</div>';
                    str+='</div>';
                    str+='<div class="wt"></div>';
                    str+='</div>';
                    str+='<div class="tiaozheng">';
                    str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_delete_btn_nor.png" class="kai_delete" onclick="deleteImg(this,'+id+')">';
                    str+='<img src="'+WST.ROOT+'/Apps/WebApp/View/default/images/photo_book_edit_the_content_adjust_position_btn_nor.png" class="kai_img"  onclick="weitiao(this,\'Five\')">';
                    str+='</div>';
                    str+='</div></div>';
                }
                $('.imgDiv').eq(page-1).append(str);
            });

        }else if(json.status==6){
            //如果是最後一页，就增加一页
            //先保存当前页+1的值
            var input= $('.gundong').val();
            //var page=Number(input)+1;
            //alert(json.newPage)
            localStorage.setItem('curPage',json.newPage);
            $.post(Think.U('WebApp/PhotoBooks/addPageInBook'),{newPage:json.newPage,photoId:id}, function(obj){
            //alert(JSON.stringify(obj))
                if(obj.status==1){

                    location.reload()
                }

            })
        }else{
            wstMsg('此页面已满');
        }
    });
    //location.reload()
}

function wstMsg(msg, focusTarget, time){
    $('#wst-modal-content').html(msg);
    $('#wst-msg-modal').show();
    $('#wst-msg-modal').addClass('am-animation-fade');
    var t = time ? time : 2;
    setTimeout(function(){
        if(focusTarget){
            $('#'+focusTarget).focus();
        }
        $('#wst-msg-modal').fadeOut(1000);
    },t*1000);
}
//删除放在预留上面的照片
function sureDelPhoto(id,param){
    $.post(Think.U('WebApp/PhotoBooks/sureDelPhoto'),{photoId:id}, function(json){
//        alert(JSON.stringify(json))
        if(json.status==1){
            $(param).parent().parent().remove();
            wstMsg('删除成功');
        }else{
            wstMsg('删除失败，请不要删除其他人的图片');
        }
    })
}